using MediatR;
using Microsoft.Extensions.Logging;
using RBAC.Domain.SalesManage;
using RBAC.ErrorCode;
using RBAC.Interstructrue.Base;
using RBAC.Write.Api.Application.Command.ERP.SalesManagement;
using RBAC.Write.Api.Utils;

namespace RBAC.Write.Api.Application.Handler.ERP.SalesManagement
{
    /// <summary>
    /// 审核/反审核/退回 销售订单 处理器
    /// </summary>
    public class ApproveOrReturnSalesOrderHandler : IRequestHandler<ApproveOrReturnSalesOrderCommand, APIResult<bool>>
    {
        private readonly IBaseRepository<SalesOrder> _salesOrderRepository;
        private readonly ILogger<ApproveOrReturnSalesOrderHandler> _logger;

        public ApproveOrReturnSalesOrderHandler(
            IBaseRepository<SalesOrder> salesOrderRepository,
            ILogger<ApproveOrReturnSalesOrderHandler> logger)
        {
            _salesOrderRepository = salesOrderRepository;
            _logger = logger;
        }

        public Task<APIResult<bool>> Handle(ApproveOrReturnSalesOrderCommand request, CancellationToken cancellationToken)
        {
            return AuditHelper.AuditOrReturnAsync<SalesOrder>(
                repo: _salesOrderRepository,
                id: request.Id,
                action: request.Action,
                getStatus: e => e.OrderStatus ?? string.Empty,
                setStatus: (e, s) => e.OrderStatus = s,
                setAuditDate: (e, d) => e.AuditDate = d,
                setAuditor: (e, u) => e.Auditor = u,
                auditorName: request.AuditorName ?? string.Empty,
                logger: _logger
            );
        }
    }
}
